/*
 * render.cpp
 * This file is part of OpenAnimator
 *
 * Copyright (C) 2008 - Abhinav Lele
 *
 * OpenAnimator is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * OpenAnimator is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with OpenAnimator; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, 
 * Boston, MA  02110-1301  USA
 */

#include "render.h" 
#include "xlua.h"


Renderer::Renderer(string mappingscriptpath) {
	T("Renderer:init")
	M(mappingscriptpath)
	script_land.load_library(LUALIB_BASE);	 script_land.load_library(LUALIB_TABLE);
	script_land.load_library(LUALIB_STRING); script_land.load_library(LUALIB_MATH);
	script_land.load_library(LUALIB_XML);  script_land.load_library(LUALIB_OPENGL);
	script_land.create_link(XLUA_renderer_get_mapping,"get_mapping");
	script_land.create_link(XLUA_renderer_set_mapping,"set_mapping");
	
	script_land.create_link(XLUA_library_new,"library_new");
	script_land.create_link(XLUA_library_get,"library_get");
	script_land.create_link(XLUA_library_set,"library_set");
	script_land.create_link(XLUA_library_del,"library_del");
	script_land.create_link(XLUA_canvas_new,"canvas_new");
	script_land.create_link(XLUA_canvas_get,"canvas_get");
	script_land.create_link(XLUA_canvas_set,"canvas_set");
	script_land.create_link(XLUA_canvas_del,"canvas_del");
	script_land.create_link(XLUA_config_new,"config_new");
	script_land.create_link(XLUA_config_get,"config_get");
	script_land.create_link(XLUA_config_set,"config_set");
	script_land.create_link(XLUA_config_del,"config_del");

	script_land.load_file(mappingscriptpath.c_str());

	script_land.call("init");
	script_land.remove_link(string("init"));
	script_land.remove_link(string("set_mapping"));
	script_land.remove_link(string("get_mapping"));
	/* tag fn map initialization done */
	bg_r = bg_g = bg_b  = bg_a = 1.0f;
	glClearColor( bg_r , bg_g , bg_b , bg_a );
	glClear(GL_COLOR_BUFFER_BIT);
	
	/* ready for rendering */
}

void Renderer::render(int frame) {
	T("Renderer:render frame="<< frame  )
	DICTITER canvas_iter;
	GENOBJ_SDATAITER gds_iter;
	MAPITER mp_iter; 
	struct generic_data typeValue;
//	glClearColor( bg_r , bg_g , bg_b , bg_a );	glClear(GL_COLOR_BUFFER_BIT);

	for(canvas_iter = myCanvas.repository.begin(); canvas_iter != myCanvas.repository.end(); canvas_iter++) {
		gds_iter = canvas_iter->second.stringStore.find("type");
		if(gds_iter == canvas_iter->second.stringStore.end()) continue;
		typeValue = gds_iter->second;	if(typeValue.isNumber) continue; /* only string mapping to dictionary allowed */
		mp_iter  = mapping_tag_fn.find(typeValue.store.sData);
		if(mp_iter == mapping_tag_fn.end()) continue;
		script_land.call(mp_iter->second,frame);
	}
	
	SDL_GL_SwapBuffers(); 	glClearColor( bg_r , bg_g , bg_b , bg_a );	glClear(GL_COLOR_BUFFER_BIT);

}
